// pages/index/order/index.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        price:0,
        freight:0,
        zongprice:0,
        id:0,
        isShowJieSuan:0,
        isweixin:0,
        isyue:0,
        uid:0,
        phone:'',
        address:'',
        liuyan:'',
        oid:0,
        datainfo:{},
        kuaidi_sn:'',
        kuaidi_com:'',
        xiyaozhifu:0,
        shuru:0
    },

    //获取订单信息
    getDataInfo:function(e){
        var that = this;
        wx.request({
            url:getApp().data.server + '/index.php/Tongcheng/Book/getShopOrderInfo?id='+that.data.oid,
            success:res=>{
                // console.log(JSON.stringify(res));
                if(0 != res.data){
                    that.setData({
                        price:res.data.price,
                        freight:res.data.yun_fei,
                        kuaidi_sn:res.data.kuaidi_sn,
                        kuaidi_com:res.data.kuaidi_com,
                        datainfo:res.data
                    })
                    if(1 == res.data.status && 2 == res.data.type){
                        that.setData({
                            xiyaozhifu:1
                        })
                    }
                }
            }
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        this.setData({
            // price:options.price,
            // freight:options.freight,
            // id:options.id,
            // isweixin:1,
            // zongprice:parseFloat(options.price)+parseFloat(options.freight),
            oid:options.id
        })
        // if(undefined != options.oid){
        //     this.setData({
        //         oid:options.oid
        //     })
        // }
        this.getDataInfo();
    },
    dataChange:function(e){
        var name = e.currentTarget.dataset.name;
        var that = this;
        switch (name){
            case 'phone':
                that.setData({
                    phone:e.detail.value
                })
                break;
            case 'address':
                that.setData({
                    address:e.detail.value
                })
                break;
            case 'remark':
                that.setData({
                    liuyan:e.detail.value
                })
                break;
            case 'kuaidi_sn':
                that.setData({
                    kuaidi_sn:e.detail.value
                })
                break;
            case 'kuaidi_com':
                that.setData({
                    kuaidi_com:e.detail.value
                })
                break;
        }
    },
    bindfocus:function(e){
      this.setData({
          shuru:1
      })
    },

//显示结算
    showJieSuan:function(e){
        var that = this;
        if('' == this.data.phone || this.data.phone.length != 11){
            wx.showModal({
                title:'提示',
                content:'请正确填写手机号'
            })
            return ;
        }
        if('' == this.data.address){
            wx.showModal({
                title:'提示',
                content:'请正确填写地址'
            })
            return ;
        }

        if(0 != this.data.uid && getApp().globalData.userInfo.uid == this.data.uid){
            wx.showModal({
                title:'提示',
                content:'无法购买自己的商品'
            })
            return ;
        }

        this.setData({
            isShowJieSuan:that.data.isShowJieSuan==1?0:1
        })
    },

    //切换支付方式
    changeZhiFuType:function(e){
        var type = e.currentTarget.dataset.type;
        if(type == 'isweixin'){
            this.setData({
                isweixin:1,
                isyue:0
            })
        }else if(type == 'isyue'){
            this.setData({
                isweixin:0,
                isyue:1
            })
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },

    querengoumai:function(e){

        var that = this;
        wx.showModal({
            title:'提示',
            content:'确认购买吗？',
            success:function(res){
                if(res.confirm){
                    that.gouMaiUnuse();
                }else if(res.cancel){
                    getApp().getUserInfo();
                }
            }
        })
    },

    //确认购买
    gouMaiUnuse:function(e){
        var that = this;

        if(1 == that.data.isyue){
            var yuenum = getApp().globalData.userInfo.yu_e_num;
            if(yuenum<that.data.zongprice){
                wx.showToast({
                    title:'余额不足',
                    icon: 'success',
                    duration:3000
                })
                return;
            }
        }
        // console.log(getApp().globalData.userInfo.openid)
        wx.request({
            url:getApp().data.server + '/index.php/Tongcheng/Book/gouMaiUnuse',
            data:{
                price:that.data.price,
                freight:that.data.freight,
                zongprice:that.data.zongprice,
                otherid:that.data.id,
                openid:getApp().globalData.userInfo.openid,
                uid:getApp().globalData.userInfo.uid,
                isyue:that.data.isyue,
                isweixin:that.data.isweixin,
                oid:that.data.oid
            },
            success:function(d){

                if(1 == that.data.isyue){
                    wx.showToast({
                        title:'购买成功',
                        icon: 'success',
                        duration:3000
                    })
                    setTimeout(function(){
                        wx.redirectTo({
                            url: '/pages/order/index?from=0'
                        })
                    },2000)
                    getApp().getUserInfo();
                    return;
                }
                console.log(d.data);
                // return;
                wx.requestPayment(
                    {
                        'timeStamp': d.data.timeStamp,
                        'nonceStr': d.data.nonceStr,
                        'package': d.data.package,
                        'signType': 'MD5',
                        'paySign': d.data.paySign,
                        'success':function(res){
                            console.log(res)
                            wx.showToast({
                                title:'购买成功',
                                icon: 'success',
                                duration:3000,
                                success:function(e){
                                    // that.showJieSuan();
                                }
                            })
                        },
                        'fail':function(res){
                            console.log(res)
                            if('requestPayment:fail cancel'==res.errMsg){
                                wx.showToast({
                                    title:'用户取消支付',
                                    icon: 'none',
                                    duration:3000,
                                    success:function(e){
                                        that.showJieSuan();
                                    }
                                })
                            }else{
                                wx.showToast({
                                    title:'支付失败',
                                    icon: 'none',
                                    duration:3000,
                                    success:function(e){
                                        that.showJieSuan();
                                    }
                                })
                            }
                        },
                        'complete':function(res){
                            // wx.showToast({
                            //     title:'购买成功',
                            //     icon: 'success',
                            //     duration:3000,
                            //     success:function(e){
                            //         that.showJieSuan();
                            //     }
                            // })
                        }
                    })

            }
        })
    },
    qurenfahuo:function(e){
      var that = this;
      var id = e.currentTarget.dataset.id;
        if(null == this.data.kuaidi_sn || '' == this.data.kuaidi_sn || this.data.kuaidi_sn.length <= 0){
            wx.showModal({
                title:'提示',
                content:'请正确填写快递单号'
            })
            return ;
        }
        if(null == this.data.kuaidi_com || '' == this.data.kuaidi_com || this.data.kuaidi_com.length <= 0){
            wx.showModal({
                title:'提示',
                content:'请正确填写快递公司'
            })
            return ;
        }
        wx.request({
            url:getApp().data.server + '/index.php/Tongcheng/Book/queRenFaHuo',
            data:{
                id:id,
                kuaidi_sn:that.data.kuaidi_sn,
                kuaidi_com:that.data.kuaidi_com,
            },
            success:function(d){
              if(1 == d.data){
                  wx.showModal({
                      title:'提示',
                      content:'发货成功'
                  })
                  setTimeout(function(){
                      wx.navigateBack();
                  },2000)

                  return;
              }else if(2 == d.data){
                  wx.showModal({
                      title:'提示',
                      content:'已发货'
                  })
                  return;
              }else{
                  wx.showModal({
                      title:'提示',
                      content:'发货失败'
                  })
                  return;
              }
            }
        })
    }

})